package com.ruanjian.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanjian.reggie.common.BaseContext;
import com.ruanjian.reggie.common.R;
import com.ruanjian.reggie.dto.OrderQuery;
import com.ruanjian.reggie.dto.OrdersDto;
import com.ruanjian.reggie.entity.OrderDetail;
import com.ruanjian.reggie.entity.Orders;
import com.ruanjian.reggie.service.OrderDetailService;
import com.ruanjian.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @GetMapping("/userPage")
    public R<Page> getOrderDetailPage(int page, int pageSize) {
        Long currentId = BaseContext.getCurrentId();
        Page<Orders> ordersPage = new Page<>(page,pageSize,false);
        orderService.page(ordersPage, new LambdaQueryWrapper<Orders>().eq(Orders::getUserId, currentId).orderByDesc(Orders::getCheckoutTime));
        List<Orders> collect = ordersPage.getRecords().stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            Long id = item.getId();
            List<OrderDetail> list = orderDetailService.list(new LambdaQueryWrapper<OrderDetail>().eq(OrderDetail::getOrderId, id));
            ordersDto.setOrderDetails(list);
            return ordersDto;
        }).collect(Collectors.toList());
        ordersPage.setRecords(collect);

        return R.success(ordersPage);
    }

    @PostMapping("/submit")
    public R<Orders> addOrderApi(@RequestBody Orders orders){
       orderService.submit(orders);
        return R.success(orders);
    }

    /**
     * 后台查询叮订单详情
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> getOrderPage(int page, int pageSize, OrderQuery orderQuery) {
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        if (orderQuery!=null){
            if(!StringUtils.isEmpty(orderQuery.getNumber())){
                wrapper.eq(Orders::getId,orderQuery.getNumber());
            }
            if(orderQuery.getBeginTime()!=null){
                wrapper.ge(Orders::getOrderTime,orderQuery.getBeginTime());
            }
            if(orderQuery.getEndTime()!=null){
                wrapper.le(Orders::getOrderTime,orderQuery.getEndTime());
            }
        }
        orderService.page(ordersPage,wrapper);
        return R.success(ordersPage);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<Orders> updateOrder(@RequestBody Orders orders){
        orderService.update(new LambdaUpdateWrapper<Orders>().eq(Orders::getId,orders.getId()).set(Orders::getStatus,orders.getStatus()));
        return R.success(orders);
    }


    /**
     * 根据订单号码在来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<Orders> orderAgainApi(@RequestBody Orders orders){
        log.info(orders.getId().toString());
        Orders again = orderService.again(orders);
        System.out.println("这是修改后的");
        log.info(orders.getId().toString());


        return R.success(again);
    }



}


